package com.supermarket.manage.business.usermanage.controller;

import com.supermarket.manage.common.dto.CodeStatus;
import com.supermarket.manage.common.dto.ResponseResult;
import com.supermarket.manage.common.pojo.Supplier;
import com.supermarket.manage.common.validation.CheckGroup;
import com.supermarket.manage.provider.api.instock.SupplierManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-13 18:01
 **/
@RestController
@RequestMapping("/supplierManage")
@Api(value = "SupplierManageController", tags = "供应商管理接口")
public class SupplierManageController {

    @Reference(version = "1.0.0")
    private SupplierManageService supplierManageService;

    /**
     * 自主供应 对应id
     */
    private static final String SELF_SUPPLIER = "646b5c5e3f1f090790e4baa64e652c68";

    @PreAuthorize("hasAuthority('/logined')")
    @PostMapping("/getSupplierList")
    @ApiOperation(value = "获取供应商列表", notes = "可根据名称、电话、地址进行模糊查询。" +
            "前端在供应商管理界面请隐藏buyerId为0000的自主供应，因为该数据不可更改及删除")
    public ResponseResult<List<Supplier>> getSupplierList(@Validated({CheckGroup.Get.class})
                                                          @RequestBody Supplier supplier) {
        List<Supplier> list = supplierManageService.getSupplierList(supplier);
        if (list != null) {
            return new ResponseResult<>(CodeStatus.OK, "查询成功", list);
        }
        return new ResponseResult<>(CodeStatus.FAIL, "查询失败");
    }

    @PreAuthorize("hasAuthority('/logined')")
    @GetMapping("/getSupplier/{sid}")
    @ApiOperation(value = "根据id获取供应商", notes = "根据id获取供应商")
    public ResponseResult<Supplier> getSupplier(@NotBlank(message = "供应商id不能为空")
                                                @PathVariable String sid) {
        Supplier supplier = supplierManageService.getSupplier(sid);
        if (supplier != null) {
            return new ResponseResult<>(CodeStatus.OK, "查询成功", supplier);
        }
        return new ResponseResult<>(CodeStatus.FAIL, "查询失败");
    }

    @PreAuthorize("hasAnyAuthority('/','/supplier/manage')")
    @PostMapping("/addSupplier")
    @ApiOperation(value = "添加供应商", notes = "必传参数：sname，sphone，adress，buyerId")
    public ResponseResult<Void> addSupplier(@Validated({CheckGroup.Add.class})
                                            @RequestBody Supplier supplier) {
        Integer count = supplierManageService.getCountBySupplier(supplier);
        if (count > 0) {
            return new ResponseResult<>(CodeStatus.FAIL, "添加失败:该供应商已存在");
        }
        Boolean aBoolean = supplierManageService.addSupplier(supplier);
        if (aBoolean) {
            return new ResponseResult<>(CodeStatus.OK, "添加成功");
        }
        return new ResponseResult<>(CodeStatus.FAIL, "添加失败");
    }

    @PreAuthorize("hasAnyAuthority('/','/supplier/manage')")
    @PostMapping("/updateSupplier")
    @ApiOperation(value = "根据id更新供应商", notes = "必传参数：sid，sname，sphone，adress，buyerId")
    public ResponseResult<Void> updateSupplier(@Validated({CheckGroup.Update.class})
                                               @RequestBody Supplier supplier) {
        Boolean aBoolean = supplierManageService.updateSupplier(supplier);
        if (aBoolean) {
            return new ResponseResult<>(CodeStatus.OK, "更新成功");
        }
        return new ResponseResult<>(CodeStatus.FAIL, "更新失败");
    }

    @PreAuthorize("hasAnyAuthority('/','/supplier/manage')")
    @GetMapping("/deleteSupplier/{sid}")
    @ApiOperation(value = "根据id删除供应商", notes = "根据id删除供应商")
    public ResponseResult<Void> deleteSupplier(@NotBlank(message = "供应商id不能为空")
                                               @PathVariable String sid) {
        if (sid.equals(SELF_SUPPLIER)) {
            return new ResponseResult<>(CodeStatus.FAIL, "删除失败:自主供应不可删除");
        }
        Boolean aBoolean = supplierManageService.deleteSupplier(sid);
        if (aBoolean) {
            return new ResponseResult<>(CodeStatus.OK, "删除成功");
        }
        return new ResponseResult<>(CodeStatus.FAIL, "删除失败");
    }

}
